###############################################################################
# Appendix Figure 13 1a / 2a
###############################################################################
# 
###############################################################################
# Content
###############################################################################
# 1) Dependencies
# 2) Load Data
# 3) Aggregation for Figure
# 4) Figure
###############################################################################
# 1) Dependencies
###############################################################################
library(readr)
library(dplyr)
library(plyr)
library(ggplot2)
library(gganimate)
library(ggeffects)
library(ggExtra)
library(ggridges)
library(ggrepel)
library(grid)
library(scales)
library(lubridate)
library(extrafont)
library(reshape2)
library(here)
library(ggforce)
library(png)
library(readxl)
library(grid)
library(gridExtra)
library(ggpubr)
library(ggalt)
library(stringr)
###############################################################################
# 2) Load Data
###############################################################################
# Set Path
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
rm(list=ls())

# Custom functions
# ggplot rescale x axis....
scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
# ggplot order over facets...
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

suppressWarnings(source('ggplot_theme_ddl.R', encoding = "UTF-8"))

# Load Data
df <- readRDS("../data/smd_ner_2015_2019_combined.RDS")
candidates_list_15 <- read.csv('../support/candidates-2015/00-Named_Entity_List_withID.csv', stringsAsFactors = F) %>% 
  as_tibble %>% mutate(id=as.character(id))
candidates_list_19 <- read.csv('../support/candidates-2019/00-Named_Entity_List_withID.csv', stringsAsFactors = F) %>% 
  as_tibble %>% mutate(id=as.character(id))

candidates_list_19 <- candidates_list_19 %>% mutate(candidacy = as.character(gsub("\\s", " ", council))) %>% 
  mutate(council = case_when(candidacy %in% c("SR", "Former Staenderat", "Former Staenderat") ~ "sr",
                             candidacy %in% c("NR", "Former Nationalrat", "Former Nationalrat") ~  "nr",
                             candidacy %in% c("SR und NR", "NR und SR") ~ "sr & nr")) %>% 
  dplyr::select(-c(candidacy))

df$year <- as.character(df$year)
df$date <- format(as.Date(df$date, "%m-%d"), format = "%m-%d")
df$fullname <- ifelse(df$fullname == "Adèle Goumaz", "Adèle Thorens Goumaz", df$fullname)
df$fullname <- ifelse(df$fullname == "Niklaus-Samuel Gugger", "Nik Gugger", df$fullname)
df$incumbent <- ifelse(df$fullname == "Philipp Müller", 1, df$incumbent)

# Remove Federal Councilors
council <- T
# Remove Party Presidents
president <- T

# Council members 2015
council_15 <- c("Ueli Maurer", "Alain Berset", "Didier Burkhalter",
                "Simonetta Sommaruga", "Eveline Widmer Schlumpf",
                "Johann Schneider-Ammann", "Doris Leuthard")

# Council members 2019
council_19 <- c("Ueli Maurer", "Alain Berset", "Ignazio Cassis",
                "Simonetta Sommaruga", "Guy Parmelin",
                "Karin Keller-Sutter", "Viola Amherd")

# Party Presidents 2015
presi_15 <- c("Toni Brunner", "Christian Levrat", "Philipp Müller",
              "Christophe Darbellay", "Regula Rytz", "Martin Bäumle", "Martin Landolt")

# Party Presidents 2019
presi_19 <- c("Albert Rösti", "Christian Levrat", "Petra Gössi",
              "Gerhard Pfister", "Regula Rytz", "Jürg Grossen", "Martin Landolt")


# Remove Council Members:
if(council == T){
  df <- df %>% dplyr::filter((year == "2015" & !fullname %in% council_15) |
                               (year == "2019" & !fullname %in% council_19))
}
# Remove Party Presidents:
if(president == T){
  df <- df %>% dplyr::filter((year == "2015" & !fullname %in% presi_15) |
                               (year == "2019" & !fullname %in% presi_19))
}
###############################################################################
# 3) Aggregations for Figure
###############################################################################
names(df)

agg1<- df %>% dplyr::group_by(year,gender,party) %>% dplyr::summarise(n = n()) %>% 
              ungroup %>% dplyr::group_by(year) %>%
              dplyr::mutate(f = n/sum(n),
                     party = ifelse(is.na(party) == T, "", party),
                     gender = ifelse(is.na(gender) == T, "", gender)) 

agg2<- df %>% dplyr::group_by(year,gender) %>% dplyr::summarise(n = n()) %>% 
              ungroup %>% dplyr::group_by(year) %>%
              dplyr::mutate(f = n/sum(n),
                     gender = ifelse(is.na(gender) == T, "", gender)) 

agg1$gender <- ifelse(agg1$gender == "f", "Women", 
                      ifelse(agg1$gender =="m", "Men", ""))
agg2$gender <- ifelse(agg2$gender == "f", "Women", 
                      ifelse(agg2$gender == "m", "Men", ""))
###############################################################################
# 4) Figure
###############################################################################
# Values for colors
values_gender <- c("Women" = "#DD2461", "Men" = "#7D7D7C")


agg1f <- agg1 %>% dplyr::filter(gender != "")
f1 <- ggplot(agg1f, aes(y = n, x=reorder_within(party,-n,year), color = gender, fill = gender)) +
  geom_bar(stat = "identity", position = position_dodge(width = .9), width = .8) +
  facet_wrap(~year, ncol = 3, scales = "free_x") +
  scale_y_continuous(labels = label_number(scale_cut = cut_short_scale())) +
  scale_x_reordered() +
  ddl_theme() +
  labs(title = 'Number of Mentions in Newspaper Articles by Party',
       y = 'Number of Mentions', x = '', color = "Gender:", fill = "Gender:") + 
  scale_color_manual(name="Gender:", values = values_gender, labels = c("Men", "Women")) +
  scale_fill_manual(name="Gender:", values = values_gender, labels = c("Men", "Women")) +
  theme(legend.position = "bottom", legend.direction = "horizontal",
        strip.background = element_blank(), strip.text = element_text(color = "black"),
        axis.text.x = element_text(angle = 0, hjust = .5, vjust = 1, size = 16),  
        axis.text.y = element_text(hjust=.5, size = 16),
        strip.text.x = element_text(size = 16),
        axis.title = element_text(size = 16),
        plot.title = element_text(size = 20),
        legend.text = element_text(size = 16),
        plot.margin = unit(c(.5,1.3,.5,.5), "cm"),
        legend.key.size = unit(1.5,"line"),
        axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5),
        panel.spacing.x=unit(2.5, "lines"))

f1 

agg2f <- agg2 %>% dplyr::filter(gender != "")
f2 <- ggplot(agg2f, aes(y = n, x=gender, color = gender, fill = gender)) +
  geom_bar(stat = "identity", position = position_dodge(width = .9), width = .8) +
  facet_wrap(~year, ncol = 3, scales = "free_x") +
  scale_y_continuous(labels = label_number(scale_cut = cut_short_scale()), breaks = c(0,25000,50000,75000,100000,125000)) +
  ddl_theme() +
  labs(title = 'Number of Mentions in Newspaper Articles',
       y = 'Number of Mentions', x = '', color = "Gender:", fill = "Gender:") + 
  scale_color_manual(name="Gender:", values = values_gender, labels = c("Men", "Women")) +
  scale_fill_manual(name="Gender:", values = values_gender, labels = c("Men", "Women")) +
  theme(legend.position = "bottom", legend.direction = "horizontal",
        strip.background = element_blank(), strip.text = element_text(color = "black"),
        axis.text.x = element_text(angle = 0, hjust = .5, vjust = 1, size = 16),  
        axis.text.y = element_text(hjust=.5, size = 16),
        strip.text.x = element_text(size = 16),
        axis.title = element_text(size = 16),
        plot.title = element_text(size = 20),
        legend.text = element_text(size = 16),
        plot.margin = unit(c(.5,1.3,.5,.5), "cm"),
        legend.key.size = unit(1.5,"line"),
        axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5),
        panel.spacing.x=unit(2.5, "lines"))

f2 

ggsave(f1, filename='../img_appendix/afigure_13_1a.png', width=12, height=7.5, dpi=300, bg = "white") 
ggsave(f2, filename='../img_appendix/afigure_13_2a.png', width=12, height=7.5, dpi=300, bg = "white") 
